package cn.hxth.dubbo.service.impl;

import cn.hxth.api.model.ProductBaseInfo;
import cn.hxth.api.service.IProductBaseInfoService;
import cn.hxth.dubbo.service.mapper.ProductBaseInfoMapper;
import com.alibaba.fastjson.JSONObject;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class ProductBaseInfoServiceImpl implements IProductBaseInfoService {
    @Resource
    private ProductBaseInfoMapper productBaseInfoMapper ;
    @Resource(name = "myRedisTemplate")
    private RedisTemplate<String,Object> redisTemplate ;
    @Override
    public boolean insertProductBaseInfo(ProductBaseInfo productBaseInfo) {
        boolean flag = this.productBaseInfoMapper.insert(productBaseInfo) ;
        return flag ;
    }

    @Override
    public boolean updateProductBaseInfo(ProductBaseInfo productBaseInfo) {
        boolean flag = this.productBaseInfoMapper.updateByPrimaryKey(productBaseInfo) ;
        return flag ;

    }

    @Override
    public boolean deleteProductBaseInfo(long pid) {
        boolean flag = this.productBaseInfoMapper.deleteByPrimaryKey(pid) ;
        return flag;
    }

    @Override
    public ProductBaseInfo getProductBaseInfo(long pid) {

        return this.productBaseInfoMapper.selectByPrimaryKey(pid);
    }

    @Override
    public void removeProductBaseInfoCache(ProductBaseInfo productBaseInfo) {
        this.redisTemplate.delete("product_base_info_"+productBaseInfo.getPid());

    }

    @Override
    public void setProductBaseInfoCache(ProductBaseInfo productBaseInfo) {
        this.redisTemplate.opsForValue().set("product_base_info_"+productBaseInfo.getPid(), JSONObject.toJSONString(productBaseInfo));
    }

    @Override
    public ProductBaseInfo getProductBaseInfoCache(long pid) {
      String jsonStr = (String) this.redisTemplate.opsForValue().get("product_base_info_"+pid);
      return JSONObject.parseObject(jsonStr,ProductBaseInfo.class) ;
    }
}
